package com.lsf.studentmanagerup.service;

import com.lsf.studentmanagerup.bean.PageBean;
import com.lsf.studentmanagerup.bean.Student;
import com.lsf.studentmanagerup.dao.StudentDao;
import com.lsf.studentmanagerup.factory.StudentDaoFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 学生信息服务层
 *
 * @author 李树飞
 */
public class StudentService {
    private StudentDao studentDao;

    public StudentService() {
        this.studentDao = StudentDaoFactory.getStudentDao();
    }

    /**
     * 检查学生是否存在
     * @param id
     * @return
     */
    public boolean checkStudent(String id) {
        try {
            Student student = studentDao.getStudentById(id);
            if (null != student) {
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取所有学生
     * @return
     */
    public List<Student> getAllStudentsService() {
        try {
            return studentDao.getAllStudents();
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分页查询
     * @param currentPageStr 当前页，从1开始
     * @param rowStr
     * @param condition
     * @return
     */
    public PageBean<Student> getStudentsByPageService(String currentPageStr, String rowStr, Map<String, String> condition) {
        //将字符串转换成int
        int currentPage = Integer.parseInt(currentPageStr);
        int rows = Integer.parseInt(rowStr);
        if (currentPage <= 0) {
            System.out.println("起始页小于等于0，改变起始页为1");
            currentPage = 1;
        }

        //创建分页对象，设置参数
        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);

        long totalCount = 0;
        List<Student> studentList = null;
        try {
            //获取总数
            totalCount = studentDao.getTotalCount(condition);
            //根据查询条件获取学生信息列表
            studentList = studentDao.getStudentsByPage((currentPage - 1) * rows, rows, condition);
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }

        //将总数和学生列表保存到PB对象
        pageBean.setTotalCount(totalCount);
        pageBean.setList(studentList);

        //保存总页码到分页对象
        pageBean.setTotalPage((totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1));

        return pageBean;
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    public boolean removeStudentByIdService(String id) {
        try {
            return studentDao.removeStudentById(Integer.parseInt(id)) > 0;
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    public boolean addStudentService(Student student) {
        try {
            return studentDao.addStudent(student) > 0;
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    public boolean updateStudentByIdService(Student student) {
        try {
            return studentDao.updateStudentById(student) > 0;
        } catch (SQLException e) {
            System.out.println("数据库连接异常");
            e.printStackTrace();
        }
        return false;
    }
}
